<?php
require_once(dirname(__FILE__) . '/../include/config.inc.php');
require_once "function.php";
//工单提交处理
$post_time = time();
$post_time2 = date("Y-m-d H:i:s", time());
$nowtime = time();
$h = date("H", time());
$h = intval($h);
$d = date("d", time());
$d = intval($d);
$nowdate = date("Ymd", time());
// 12点自动签退
//========= 12点执行一次 ============
// 列出系统中 未签退的 工程
if ($h == 23) {
    $sql_order_2 = "SELECT *  FROM  `#@__project`  where  jindu='4'  and signIn=1 and signInTime>0  ";
    $dosql->Execute($sql_order_2);
    while ($row2 = $dosql->GetArray()) {
        $orderid = $row2['id'];
        echo "【未签退工程】========单号" . $orderid . "=========<br>";
        $mids = $row2['manageid'];
        $pmid = $row2['pmid'];
        $old_fee = $row2['fee'];
        $signInTime = $row2['signInTime'];
        $old_apply_device = $row2['apply_device'];
        //自动签退处理
        //修改订单状态
        $dosql->ExecNoneQuery("update  `#@__project` set signIn='0',signInTime=0 where id='$orderid' ");
        $remark = "项目经理未签退，系统自动施工签退";
        //记录工程操作
        $dosql->ExecNoneQuery("insert into `#@__project_log`(orderid,op,op_time,jindu,manageid,qiantui,pmid,remark) values ('$orderid','项目经理未签退，系统自动施工签退','$nowtime','4','$mids','1','$pmid','$remark')");
        //增加 任务奖励
        $m2_arr = explode(",", $mids);
        foreach ($m2_arr as $key => $val) {
            //计算 费用
            $fee = autoSumFee($old_fee, $signInTime, $cfg_jiabanfei);
            //插入 奖励记录
            $dosql->ExecNoneQuery("insert into `#@__fee_log`(`type`,orderid,op,op_time,jindu,manageid,remark,fee,task) values ('project','$orderid','add','$nowtime','4','$val','项目经理未签退，系统自动施工签退 计算奖励','$fee','$old_apply_device')");
            //增加奖励计划
            $dosql->ExecNoneQuery("update  `#@__user` set fee=fee+'$fee' where id='$val' ");
            //插入 奖励记录
            $fee2=100;
            $dosql->ExecNoneQuery("insert into `#@__fee_log`(`type`,orderid,op,op_time,jindu,manageid,remark,fee,task) values ('project','$orderid','move','$nowtime','4','$val','项目经理未签退，系统自动施工签退 扣除100积分','$fee2','$old_apply_device')");
            //增加奖励计划
            $dosql->ExecNoneQuery("update  `#@__user` set fee=fee-'$fee2' where id='$val' ");
        }
        echo "【工单记录 end 】===========================<br><br><br>";
    }
    // 找到施工逾期的工程 对项目进行 处罚
    $sql_order_3 = "SELECT *  FROM  `#@__project`  where   jindu='4'  and is_delete=0 and  endtime+86400<$nowtime  ";
    $dosql->Execute($sql_order_3, 33);
    while ($row3 = $dosql->GetArray(33)) {
        $project_id = $row3['id'];
        $pmid = $row3['pmid'];
        $old_apply_device = $row3['apply_device'];
        echo "<br>逾期id:" . $project_id . "<br>";
        $fee_arr = $dosql->GetOne("select `id` from `#@__fee_log` where manageid='$pmid' and orderid='$project_id' and feetype='shigongyuqi' and nowdate='$nowdate'  ");
        if (!$fee_arr) {
            $dosql->ExecNoneQuery("insert into `#@__fee_log`(`type`,orderid,op,op_time,jindu,manageid,remark,fee,task,feetype,nowdate) values ('project','$project_id','move','$nowtime','4','$pmid','项目施工逾期，项目经理 处罚','$cfg_yanqijungong','$old_apply_device','shigongyuqi','$nowdate')");
            //增加奖励计划
            $dosql->ExecNoneQuery("update  `#@__user` set fee=fee-'$cfg_yanqijungong' where id='$pmid' ");
        }
    }
    // 找到 验收 预期的工程 对项目进行 处罚
    $sql_order_4 = "SELECT *  FROM  `#@__project`  where  jindu='6'  and is_delete=0 and over_time>0 and  oktime2>0 and oktime2<$nowtime  ";
    $dosql->Execute($sql_order_4, 44);
    while ($row3 = $dosql->GetArray(44)) {
        $project_id = $row3['id'];
        $pmid = $row3['pmid'];
        $old_apply_device = $row3['apply_device'];
        $fee_arr = $dosql->GetOne("select `id` from `#@__fee_log` where manageid='$pmid' and orderid='$project_id' and feetype='yanshouyuqi' and nowdate='$nowdate'  ");
        if (!$fee_arr) {
            $dosql->ExecNoneQuery("insert into `#@__fee_log`(`type`,orderid,op,op_time,jindu,manageid,remark,fee,task,feetype,nowdate) values ('project','$project_id','move','$nowtime','4','$pmid','项目验收预期，项目经理 处罚','$cfg_yanqiyanshou','$old_apply_device','yanshouyuqi','$nowdate')");
            //增加奖励计划
            $dosql->ExecNoneQuery("update  `#@__user` set fee=fee-'$cfg_yanqiyanshou' where id='$pmid' ");
        }
    }
}
if (date("w") == "0" && $h == 23) {
    $sdefaultDate = date("Y-m-d");
    //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
    $first = 1;
    //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
    $w = date('w', strtotime($sdefaultDate));
    //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
    $week_start = date('Y-m-d', strtotime("$sdefaultDate -" . ($w ? $w - $first : 6) . ' days'));
    $week_start_time = strtotime("$sdefaultDate -" . ($w ? $w - $first : 6) . ' days');
    //本周结束日期
    $week_end = strtotime("$week_start +6 days") + 86399;
    $sql_order_5 = "SELECT *  FROM  `#@__project`  where  jindu='4'  and is_delete=0    ";
    $dosql->Execute($sql_order_5, 55);
    while ($row5 = $dosql->GetArray(55)) {
        $project_id = $row5['id'];
        $pmid = $row5['pmid'];
        $old_apply_device = $row5['apply_device'];
        //本周是否上传周汇报
        $report_Arr = $dosql->GetOne("select `id` from  `#@__project_log`  WHERE orderid='$project_id' and wreport=1 and op_time>$week_start_time and op_time<$week_end  ");
        if (is_array($report_Arr) && isset($report_Arr['id'])) {
            echo $report_Arr['id'];
        } else {
            //进行积分操作
            $fee_arr = $dosql->GetOne("select `id` from `#@__fee_log` where manageid='$pmid' and orderid='$project_id' and feetype='jinduhuibao' and nowdate='$nowdate'  ");
            if (!$fee_arr) {
                $dosql->ExecNoneQuery("insert into `#@__fee_log`(`type`,orderid,op,op_time,jindu,manageid,remark,fee,task,feetype,nowdate) values ('project','$project_id','move','$nowtime','4','$pmid','未上传本周的进度汇报','$cfg_shigonghuibao','$old_apply_device','jinduhuibao','$nowdate')");
                //增加奖励计划
                $dosql->ExecNoneQuery("update  `#@__user` set fee=fee-'$cfg_shigonghuibao' where id='$pmid' ");
            }
        }
    }
}
/*
 自动计算加班费
int $fee   费用标准
int $signInTime   签到时间
int $cfg_jiabanfei 加班每小时奖励
*/
function autoSumFee($fee, $signInTime, $cfg_jiabanfei)
{
    // 计算 费用
    // 获取最后一次签到时间
    $getFee = 0;
    $time_now = time();
    //计算 施工时间
    $hour_now = date("H", time());
    //加班
    $hour_18 = date("Y-m-d 18:00:00");
    $fenge_time = strtotime($hour_18);
    // 加班情况
    if ($hour_now < 18) {
        //没有加班
        $min_count = ($time_now - $signInTime) / 60; //施工分钟数
        $min_fee = $fee / 600; //每分钟的数值
        $getFee = ceil($min_count * $min_fee);
    } else {
        //加班跨时段
        if ($signInTime < $fenge_time) {
            //正常时段
            //精确到分钟
            $min_count = ($fenge_time - $signInTime) / 60; //施工分钟数
            $min_fee = $fee / 600; //每分钟的数值
            $getFee = ceil($min_count * $min_fee);
            if ($getFee > 260) {
                $getFee = 260;
            }
            //加班时段
            $hour2 = ($time_now - $fenge_time) / 3600;
            $hour2 = round($hour2, 2);
            // 加班时间最多按4个小时算
            //不足4小时的，精确到分钟
            if ($hour2 < 4) {
                $getFee += ceil(($time_now - $fenge_time) * ($cfg_jiabanfei / 3600));
            } else {
                $getFee += $cfg_jiabanfei * 4;
            }
        } else {
            $getFee = ceil(($time_now - $signInTime) * ($cfg_jiabanfei / 3600));
        }
    }
    return $getFee;
}


//========= 签到后反馈的任务 ============
// 列出系统中 签到后反馈的 任务
// 先签到再完成或者反馈  接单2小时后签到
if ($h == 23) {


    $sql_order = "SELECT id,manageid,post_time,apply_device,content,school,truename,mobile  FROM  `#@__order`  where    jindu='4'   and is_delete=0  and qiandao='true'  order by id desc   ";
    $dosql->Execute($sql_order);
    while ($row = $dosql->GetArray()) {
        $orderid = $row['id'];
        echo "id: " . $orderid . "  已经签到没有结束的订单<br>";
        $mids = $row['manageid'];
        $content = $row['content'];
        $orderidname = date('Ymd', $row['post_time']) . "-" . $orderid;  //工单号
        $repairname = getRepairMode($row['apply_device']);  //任务名称
        $task = $row['apply_device'];  //任务名称
        $apply_time = date("Y-m-d H:i:s", $row['post_time']);
        $school = $row['school'];
        $truename = $row['truename'];
        $mobile = $row['mobile'];
        //执行人
        $zhixingren = "";
        $ids_arr = explode(",", $mids);
        foreach ($ids_arr as $key => $val) {
            $zhixingren .= getManage($val) . "    ";
        }
        //查找 接单 日志
        // 如果存在反馈的信息，则不执行
        $row_order = $dosql->GetOne("SELECT *  FROM  `#@__order_log`  where   orderid='$orderid'   AND jindu='4'    order by id desc   ");
        if (is_array($row_order)) {


            $last_time = $row_order['op_time'];
            $is_fankui = $row_order['fankui'];


            //接单后  反馈  然后  第二天需要重新反馈的
            //消息推送
            $last_time_d = date("d", $last_time);
            echo "日期：" . $last_time_d;
            if ($last_time_d != $d) {
                //判断是否 推送消息
                $row_timeout = $dosql->GetOne("SELECT *  FROM  `#@__timeout_log`  where   orderid='$orderid'   AND   jindu='4' AND  sendmsg='true' and `type`='fankui'   order by id desc   ");
                if (is_array($row_timeout) && isset($row_timeout['id']) && date('Y-m-d', $row_timeout['op_time']) == date("Y-m-d")) {
                    // 扣除奖励
                    //判断 已经扣费
                    $row_fee = $dosql->GetOne("SELECT *  FROM  `#@__fee_log`  where   orderid='$orderid'  AND jindu='4'  AND op='move'  order by id desc   ");
                    if (is_array($row_fee) && isset($row_fee['id']) && date('Y-m-d', $row_fee['op_time']) == date("Y-m-d")) {

                    } else {
                        //首次超时 ，记录入库 并发送消息
                        foreach ($ids_arr as $key => $val) {
                            //获取工程师 openid
                            $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                            $openid_str = $m['openid'];
                            //插入更新的状态
                            $remark_fee = '反馈工单次日未继续反馈，扣除奖励' . $cfg_doingoutfee . '。';
                            $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark,`type`,task,feetype) values ('$orderid','move','$post_time','4','$val','$cfg_doingoutfee','$remark_fee','order','$task','fankuichaoshi')");
                        }
                    }


                }


            }
        }

    }
}


?>
